<template>
	<sc-dialog v-model="visible" :title="titleMap[mode]" show-fullscreen destroy-on-close width="750px" @close="close">
		<el-form ref="formRef" label-width="120px" :model="formData" :rules="rules">
			<el-form-item label="工单单号" prop="wo">
				<el-input
					v-model.trim="formData.wo"
					placeholder="请输入工单单号"
					:maxlength="32"
					show-word-limit
					:disabled="mode == 'edit'"
					clearable>
				</el-input>
			</el-form-item>
			<el-form-item label="产品" prop="productId">
				<MaterialTableSelect v-model="selectedProduct" placeholder="请选择产品" @change="change" />
			</el-form-item>
			<el-form-item label="总数量" prop="totalQty">
				<el-input-number v-model="formData.totalQty" :min="1" :max="99999" :step="100" />
			</el-form-item>
			<el-form-item label="计划开始时间" prop="planStartTime">
				<el-date-picker v-model="formData.planStartTime" type="date" placeholder="请输入计划开始时间" />
			</el-form-item>
			<el-form-item label="计划结束时间" prop="planEndTime">
				<el-date-picker v-model="formData.planEndTime" type="date" placeholder="请输入计划结束时间" />
			</el-form-item>
			<el-form-item label="工单来源" prop="source">
				<el-radio-group v-model="formData.source">
					<el-radio
						v-for="item in sourceOptions"
						:key="item.value"
						:label="item.value"
						>{{ item.label }}</el-radio
					>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="批次号" prop="batchNo">
				<el-input
					v-model.trim="formData.batchNo"
					placeholder="请输入批次号"
					:maxlength="64"
					show-word-limit
					clearable>
				</el-input>
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">确 定</el-button>
		</template>
	</sc-dialog>
</template>

<script>
import MaterialTableSelect from "../../components/MaterialTableSelect";

export default {
	emits: ['complete'],
	components: {
		MaterialTableSelect,
	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			isSaveing: false,
			visible: false,
			sourceOptions: [],
			selectedProduct: {
				id: 0,
				itemName: '',
			},
			formData: {
				id: 0,
				wo: '',
				productId: undefined,
				productCode: '',
				totalQty: 0,
				planStartTime: undefined,
				planEndTime: undefined,
				source: undefined,
				batchNo: '',
			},
			rules: {
				wo: [{ required: true, message: "请输入工单单号", trigger: "blur" }],
				productId: [{ required: true, message: "请选择产品", trigger: "blur" }],
				totalQty: [{ required: true, message: "请输入总数量", trigger: "blur" }],
				planStartTime: [{ required: true, message: "请输入计划开始时间", trigger: "blur" }],
				planEndTime: [{ required: true, message: "请输入计划结束时间", trigger: "blur" }],
				source: [{ required: true, message: "请选择工单来源", trigger: "blur" }],
			},

		};
	},
	methods: {
		change(item) {
			this.formData.productId = item.id;
			this.formData.productCode = item.itemCode;
		},
		async open(row) {
			if (!row) {
				this.mode = "add";
			} else {
				this.mode = "edit";
				let resp = await this.$API.prodwo.model.get(row.id);
				this.formData = resp.data;

				this.selectedProduct = { id: this.formData.productId, itemName: this.formData.product.itemName };
			}
			const resp2 = await this.$API.enumlist.model.get('woSourceEnum');
			this.sourceOptions = resp2.data;
			this.visible = true;
		},
		save() {
			this.$refs.formRef.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let resp = null;
					if (this.formData.id === 0) {
						resp = await this.$API.prodwo.add.post(this.formData);
					} else {
						resp = await this.$API.prodwo.update.put(this.formData);
					}
					this.isSaveing = false;
					if (resp.code == 200) {
						this.$emit("complete");
						this.visible = false;
						this.$message.success("保存成功");
					} else {
						this.$alert(resp.message, "提示", { type: "error" });
					}
				}
			});
		},
		close() {
			this.selectedProduct = {};
			this.formData = {
				id: 0,
				wo: '',
				productId: undefined,
				productCode: '',
				totalQty: 0,
				planStartTime: undefined,
				planEndTime: undefined,
				source: undefined,
				batchNo: '',
			};
			this.$refs.formRef.resetFields();
			this.visible = false;
		},
	},
};
</script>
